<script type="text/javascript">
/* Page init setting for User */
KE.show({
    id : 'content',
    cssPath : '<?= Zend_Registry::get('baseDir')?>/Common/Layout/KindEditor.css'
});
var initUrl = "<?= Zend_Registry::get('baseUrl')?>/userstory/query";
var initParam = "currentpage=1";
var columnName = new Array("userstoryid", "milestone", "headline");
var operate = "edit,delete,newspec,newtc";
var currentPage = "1";

$(document).ready(function() {
	fillData(initUrl, initParam, columnName, operate, currentPage);
	markDisabled(Array("edit", "assignee"));
	$("#status").attr("selectedIndex", 1);
	$("#priority").attr("selectedIndex", 1);
	$("#milestone").attr("selectedIndex", 1);
	$("#module").attr("selectedIndex", 1);
	
	$("#assignee").toggle(
		function() {
			$("#assigneePanel").show();
			fillUserstoryAssignee();
		},
		function() {
			$("#assigneePanel").hide();
	});
});

/* Fill data to action panel for Edit */
function goEdit(obj, isEdit) {
	var dataRow = $(obj).parent().parent().children();
	var param = "userstoryid=" + dataRow.eq(0).text();
	$.post(initUrl, param, function (data) {
		var json = eval(data);
    	var lenJson = json.length - 1;
    	if (lenJson == 1) {
        	$("#userstoryid").val(json[0]['userstoryid']);
        	$("#creator").val(json[0]['creator']);
        	$("#proposalid").val(json[0]['proposalid']);
        	if (json[0]['proposalid'] == 0)
        		$("#proposalid").val('');
        	$("#priority").val(json[0]['priority']);
        	$("#milestone").val(json[0]['milestone']);
        	$("#module").val(json[0]['module']);
        	$("#status").val(json[0]['status']);
        	$("#finishdate").val(json[0]['finishdate']);
        	$("#headline").val(json[0]['headline']);
        	setKEContent('content', json[0]['content']);
        	fillUserstoryAssignee();
        	markEnabled(Array("assignee"));
    	}
    	else {
        	$("#msg").text("<?= $this->translate('message.common.nodata')?>");
    	}
	});
	if (isEdit) {
		markEnabled(Array("edit"));
		markDisabled(Array("add", "userstoryid"));
	}
	else {
		markDisabled(Array("edit"));
		markEnabled(Array("add", "userstoryid"));
	}
	$("#msg").text("<?= $this->translate('tip.common.action')?>");
	$("#actionPanel").show();
	$("#assigneePanel").hide();
	location.href = "#top";
}

/* Add new data */
function doAdd() {
	var content = replaceAmp(KE.util.getData('content'));
    if (isNull("headline")) {
		$("#msg").text("<?= $this->translate('message.userstory.headline.null')?>");
		return false;
	}
    if ($.trim(content) == "") {
		$("#msg").text("<?= $this->translate('message.userstory.content.null')?>");
		KE.util.focus('content');
		return false;
	}
	
//    var assignee = ",";
//    $("input[type=checkbox][name=member][checked]").each(function(){    
//    	assignee += $(this).attr("id") + ",";    
//	});
//	var lastIndex = assignee.lastIndexOf(",");
//	var assignee = assignee.substr(0, lastIndex);

	var url = "<?= Zend_Registry::get('baseUrl')?>/userstory/add";
	var param = "proposalid=" + $.trim($("#proposalid").val()) + 
			"&priority=" + $.trim($("#priority").val()) + 
			"&milestone=" + $.trim($("#milestone").val()) + 
			"&module=" + $.trim($("#module").val()) + 
			"&status=" + $.trim($("#status").val()) + 
			"&finishdate=" + $.trim($("#finishdate").val()) + 
			"&headline=" + $.trim($("#headline").val()) + 
			"&content=" + content;
    $.post(url, param, function (data) {
    	switch (data) {
    	case "no_permission":
    		$("#msg").text("<?= $this->translate('message.common.noperm.add')?>");
    		break;
    	case "userstory_exist":
    		$("#msg").text("<?= $this->translate('message.userstory.headline.exist')?>");
    		$("#headline").focus();
    		break;
    	case "error":
    		$("#msg").text("<?= $this->translate('message.common.add.failed')?>" + "Null");
    		break;
    	default:
    		fillData(initUrl, initParam, columnName, operate, currentPage);
    		$("#userstoryid").val(data);
    		$("#msg").text("<?= $this->translate('message.common.add.successful')?>" + data);
    	}
    });
}

/* Edit existing data */
function doEdit() {
	var content = replaceAmp(KE.util.getData('content'));
	if (isNull("headline")) {
		$("#msg").text("<?= $this->translate('message.userstory.headline.null')?>");
		return false;
	}
    if ($.trim(content) == "") {
		$("#msg").text("<?= $this->translate('message.userstory.content.null')?>");
		KE.util.focus('content');
		return false;
	}
	
//    var assignee = "";
//    $("input[type=checkbox][name=member][checked]").each(function(){
//    	assignee += $(this).attr("id") + ",";
//	});
//	var lastIndex = assignee.lastIndexOf(",");
//	var assignee = assignee.substr(0, lastIndex);
	
    var userstoryid = $.trim($("#userstoryid").val());
	var url = "<?= Zend_Registry::get('baseUrl')?>/userstory/edit";
	var param = "userstoryid=" + userstoryid + 
        	"&proposalid=" + $.trim($("#proposalid").val()) + 
        	"&priority=" + $.trim($("#priority").val()) + 
        	"&milestone=" + $.trim($("#milestone").val()) + 
        	"&module=" + $.trim($("#module").val()) + 
        	"&status=" + $.trim($("#status").val()) +
        	"&finishdate=" + $.trim($("#finishdate").val()) + 
        	"&headline=" + $.trim($("#headline").val()) + 
        	"&content=" + content;
    $.post(url, param, function (data) {
    	switch (data) {
    	case "no_permission":
    		$("#msg").text("<?= $this->translate('message.common.noperm.edit')?>");
    		break;
    	case "userstory_exist":
    		$("#msg").text("<?= $this->translate('message.userstory.headline.exist')?>");
    		$("#headline").focus();
    		break;
    	case "1":
    		fillEdit(initUrl, "userstoryid=" + userstoryid, columnName, userstoryid);
    		$("#msg").text("<?= $this->translate('message.common.edit.successful')?>" + $("#userstoryid").val());
    		break;
    	case "error":
    		$("#msg").text("<?= $this->translate('message.common.edit.failed')?>" + $("#userstoryid").val());
    		break;
        default:
    		$("#msg").text(data);
    	}
	});
}

/* Delete data according to ID */
function doDelete(obj) {
	if(!confirm("<?= $this->translate('message.common.delete.confirm')?>"))
		return false;
	var dataRow = $(obj).parent().parent().children();
	var userstoryid = $.trim(dataRow.eq(0).text());
	url = "<?= Zend_Registry::get('baseUrl')?>/userstory/delete";
	param = "userstoryid=" + userstoryid;
	$.post(url, param, function(data) {
		switch (data) {
		case "no_permission":
    		$("#msg").text("<?= $this->translate('message.common.noperm.delete')?>");
    		break;
    	case "1":
    		$("#msg").text("<?= $this->translate('message.common.delete.successful')?>" + userstoryid);
			dataRow.remove();
			$("#totalRecord").text($("#totalRecord").text() - 1);
    		break;
    	case "error":
    		$("#msg").text("<?= $this->translate('message.common.delete.failed')?>" + userstoryid);
    		break;
    	default:
    		$("#msg").text(data);
    	}
	});
}

/* Search data */
function doSearch(isSearch, currentPage) {
	if (isSearch) {
		$("#cacheDiv").removeData("cacheData");
		var cacheArray = new Array();
		cacheArray['userstoryid'] = $.trim($("#userstoryid").val());
		cacheArray['creator'] = $.trim($("#creator").val());
		cacheArray['proposalid'] = $.trim($("#proposalid").val());
		cacheArray['priority'] = $.trim($("#priority").val());
		cacheArray['milestone'] = $.trim($("#milestone").val());
		cacheArray['module'] = $.trim($("#module").val());
		cacheArray['status'] = $.trim($("#status").val());
		cacheArray['finishdate'] = $.trim($("#finishdate").val());
		cacheArray['headline'] = $.trim($("#headline").val());
		cacheArray['content'] = replaceAmp($.trim(KE.util.getPureData('content')));
		$("#cacheDiv").data("cacheData", cacheArray);
	}
	var cacheData = $("#cacheDiv").data("cacheData");
	if (typeof(cacheData) == "undefined")
		var param = "currentpage=" + currentPage;
	else
		var param = "currentpage=" + currentPage +
				"&userstoryid=" + cacheData['userstoryid'] + 
		    	"&creator=" + cacheData['creator'] + 
		    	"&proposalid=" + cacheData['proposalid'] + 
		    	"&priority=" + cacheData['priority'] +
		    	"&milestone=" + cacheData['milestone'] +
		    	"&module=" + cacheData['module'] +
		    	"&status=" + cacheData['status'] +
		    	"&finishdate=" + cacheData['finishdate'] + 
		    	"&headline=" + cacheData['headline'] +
		    	"&content=" + cacheData['content'];
	fillData(initUrl, param, columnName, operate, currentPage);
	$("#msg").text("<?= $this->translate('tip.common.action')?>");
}

///* Select assignee for userstory */
//function doSelect() {
//	var assignee = "";
//	$("input[type=checkbox][name=member][checked]").each(function(){    
//		assignee += $(this).attr("value") + ",";    
//	});
//	var lastIndex = assignee.lastIndexOf(",");
//	$("#assignee").val(assignee.substr(0, lastIndex));
//	$("#memberPanel").hide();
//}

/* Fill data to member panel */
function fillUserstoryAssignee() {
	$("#assignee").val("");
	var url = "<?= Zend_Registry::get('baseUrl')?>/task/assignee";
	var	param = "refertype=userstory" + 
				"&referid=" + $.trim($("#userstoryid").val());
	$.post(url, param, function (data) {
			var json = eval(data);
	    	var lenJson = json.length;
	    	var showData = "";
	    	if (lenJson > 0) {
	    		for (i = 0; i < lenJson; i++) {
    		    	showData += "<tr>";
    		    	showData += "<td>" + json[i]["userid"] + "</td>";
    		    	showData += "<td>" + json[i]["username"] + "</td>";
    		    	showData += "<td>" + json[i]["rolename"] + "</td>";
    		    	showData += "<td><label onclick='userstoryAssigneeDelete(" + json[i]["userid"] + ")'><?= $this->translate('common.button.delete')?></label></td>";
    	    		showData += "</tr>";
    	    		$("#assignee").val($("#assignee").val() + json[i]['username'] + ",");
	    		}
	    		lastIndex = $("#assignee").val().lastIndexOf(",");
	    		$("#assignee").val($("#assignee").val().substr(0, lastIndex));
	    	}
	    	else {
		    	showData += "<tr><td><?= $this->translate('message.common.nodata')?></td></tr>";
		    	$("#msg").text("<?= $this->translate('tip.common.action')?>");
	    	}
	    	$("#assigneeList").html(showData);
	});
}

function userstoryAssigneeAdd() {
	if (isNull("userid")) {
		$("#msg").text("<?= $this->translate('message.user.username.null')?>");
    	return false;
	}
	var url = "<?= Zend_Registry::get('baseUrl')?>/task/add";
	var param = "refertype=userstory" + 
				"&referid=" + $.trim($("#userstoryid").val()) +
				"&userid=" + $.trim($("#userid").val());
	$.post(url, param, function(data) {
		switch (data) {
    	case "task_exist":
    		$("#msg").text("<?= $this->translate('message.task.exist')?>");
    		break;
    	case "error":
    		$("#msg").text("<?= $this->translate('message.common.add.failed')?>" + "Null");
    		break;
    	default:
    		fillUserstoryAssignee();
    		$("#msg").text("<?= $this->translate('message.common.add.successful')?>" + $("#userid").val());
    	}
	});
}

function userstoryAssigneeDelete(userid) {
	var url = "<?= Zend_Registry::get('baseUrl')?>/task/delete";
	var param = "refertype=userstory" + 
				"&referid=" + $.trim($("#userstoryid").val()) +
				"&userid=" + userid;
	$.post(url, param, function(data) {
		switch (data) {
    	case "1":
    		$("#msg").text("<?= $this->translate('message.common.delete.successful')?>" + userid);
    		fillUserstoryAssignee();
    		break;
    	default:
    		$("#msg").text("<?= $this->translate('message.common.delete.failed')?>" + userid);
		}
	});
}

/* Link to Spec page to new Specification */
function newSpecification(param) {
	var data = $(param).parent().parent().children();
	var userstoryid = data.eq(0).text();
	location.href = "<?= Zend_Registry::get('baseUrl')?>/spec/index/userstoryid/" + userstoryid;
}

/* Link to Testcase page to new Testcase */
function newTestCase(param) {
	var data = $(param).parent().parent().children();
	var userstoryid = data.eq(0).text();
	location.href = "<?= Zend_Registry::get('baseUrl')?>/testcase/index/userstoryid/" + userstoryid;
}

/* Reset KE content */
function resetKE() {
	setKEContent("content", "");
}
</script>

<br />
<table class="table-action">
<thead>
<tr>
	<a name="top"></a>
	<td colspan="5">
	    <?= $this->translate('common.page.panel')?>:&nbsp;&nbsp;
	    <label id="hideShow" />
	</td>
</tr>
</thead>
<tbody id="actionPanel">
<tr>
	<td width="14%"><?= $this->translate('column.common.id')?>:</td>
	<td width="36%"><input type="text" id="userstoryid" class="short" title="<?= $this->translate('tip.common.forsearch')?>" /></td>
	<td width="15%"><?= $this->translate('column.userstory.creator')?>:</td>
	<td width="25%" align="right"><input type="text" id="creator" value="<?= $_SESSION['userName']?>" class="short" title="<?= $this->translate('tip.common.forsearch')?>" /></td>
	<td width="10%"></td>
</tr>
<tr>
	<td><?= $this->translate('column.userstory.proposal')?>:</td>
	<td>
	<select id="proposalid" class="short">
		<option value=""></option>
		<?php
		 	$common = new Common();
	        foreach ($this->proposal as $proposal) {
	            if ($proposal['proposalid'] == $this->proposalId)
	               echo '<option value="' , $proposal['proposalid'] , '" selected>' , $proposal['proposalid'] , ' - ' , $common->subString($proposal['headline'], 26) , '</option>';
	            else 
	                echo '<option value="' , $proposal['proposalid'] , '">' , $proposal['proposalid'] , ' - ' , $common->subString($proposal['headline'], 26) , '</option>';
	        }
		?>
	</select>
	</td>
	<td><?= $this->translate('column.userstory.priority')?>:</td>
	<td align="right">
	<select id="priority" class="short">
		<option value=""></option>
			<?php
	        $common = new Common();
	        $usPriority = $common->parseDefinition('userstory-priority');
		    foreach ($usPriority as $up) {
                echo '<option value="' , $up , '">' , $up , '</option>';
		    }
	    ?>
	</select>
	</td>
	<td></td>
</tr>
<tr>
	<td><?= $this->translate('column.userstory.milestone')?>:</td>
	<td>
	<select id="milestone" class="short">
		<option value=""></option>
	    <?php
		    foreach ($this->milestone as $milestone) {
                echo '<option value="' , $milestone['msname'] , '">' , $milestone['msname'] , '</option>';
		    }
	    ?>
	</select>
	</td>
	<td><?= $this->translate('column.userstory.module')?>:</td>
	<td align="right">
	<select id="module" class="short">
		<option value=""></option>
	    <?php
		    foreach ($this->module as $module) {
                echo '<option value="' , $module['modname'] , '">' , $module['modname'] , '</option>';
		    }
	    ?>
	</select>
	</td>
	<td></td>
</tr>
<tr>
	<td><?= $this->translate('column.userstory.status')?>:</td>
	<td>
	<select id="status" class="short">
		<option value=""></option>
			<?php
	        $common = new Common();
	        $usStatus = $common->parseDefinition('userstory-status');
		    foreach ($usStatus as $us) {
                echo '<option value="' , $us , '">' , $us , '</option>';
		    }
	    ?>
	</select>
	</td>
	<td><?= $this->translate('column.userstory.finishdate')?>:</td>
	<td align="right"><input type="text" id="finishdate" maxlength="10" class="short" value="<?= date('Y-m-d', time() + 3600 * 24 * 30)?>" onclick="calendar(this, false)" /></td>
	<td></td>
</tr>
<tr>
	<td><?= $this->translate('column.userstory.assignee')?>:</td>
	<td colspan="3">
	<input type="text" id="assignee" maxlength="250" class="long" value="<?= $this->translate('tip.userstory.assignee.operate')?>" readonly />
	<div id="assigneePanel" class="itemlist">
		<!-- <?php
		    foreach ($this->member as $member) {
			    echo '<input type="checkbox" class="checkbox" name="member" id="' , $member['userid'] , '" value="' , $member['username'] , '" />&nbsp;&nbsp;' , $member['username'] , '<br />';
		    }
		?><hr />
		<input type="button" onclick="doSelect()" value="<?= $this->translate('common.button.ok')?>" />&nbsp;&nbsp;
		<input type="button" onclick="$('#memberPanel').hide()" value="<?= $this->translate('common.button.cancel')?>" />
		-->
		<table class="table-embedded">
			<thead>
			<tr>
				<th width="20%"><?= $this->translate('column.common.id')?></th>
				<th width="30%"><?= $this->translate('column.user.username')?></th>
				<th width="30%"><?= $this->translate('column.user.role')?></th>
				<th width="20%"><?= $this->translate('column.common.operate')?></th>
			</tr>
			</thead>
			<tbody id="assigneeList"></tbody>
			<tr>
				<td><strong><?= $this->translate('tip.userstory.assignee.add')?>:</strong></td>
				<td colspan="2">
					<select id="userid" class="long">
					<?php foreach ($this->user as $user)
						echo '<option value="' , $user['userid'] , '">' , $user['username'] , '</option>';
					?>
					</select>
				</td>
				<td><input type='button' onclick='userstoryAssigneeAdd()' value=<?= $this->translate('common.button.add')?> /></td>
			</tr>
		</table>
	</div>
	</td>
	<td></td>
</tr>
<tr>
	<td><?= $this->translate('column.userstory.headline')?>:</td>
	<td colspan="3"><input type="text" id="headline" maxlength="250" class="long" /></td>
	<td></td>
</tr>
<tr>
	<td><?= $this->translate('column.userstory.content')?>:</td>
	<td colspan="3"><textarea id="content" class="long"><?= $this->proposalContent?></textarea></td>
	<td></td>
</tr>
</tbody>
<tfoot>
<tr>
	<td><?= $this->translate('common.page.operate')?>:</td>
	<td><span id="actionButton" /></td>
	<td colspan="3">
		<span id="msg" class="message"><?= $this->translate('tip.common.action')?></span>
		<span id="loading" class="loading"><img src="<?= Zend_Registry::get('baseDir')?>/Common/Image/loading.gif" /></span>
	</td>
</tr>
</tfoot>
</table>

<br />
<table class="table-data" id="dataTable">
<thead>
<tr>
    <th width="8%" class="id"><?= $this->translate('column.common.id')?></th>
    <th width="19%"><?= $this->translate('column.userstory.milestone')?></th>
    <th width="40%"><?= $this->translate('column.userstory.headline')?></th>
    <th width="33%"><?= $this->translate('column.common.operate')?></th>
</tr>
</thead>
<tbody id="dataPanel"></tbody>
<tfoot id="footer"></tfoot>
</table>